import { defineStore } from "pinia";
import { main, getAddress } from "../service/home";

interface GlobalAreaType {
  id: string, label: string
}
interface SearchStateType {
  searchType: "1" | "2" | "3" | "4",
  orgType: any[],
  professionType: any[],
  addressTree: any[],
  globalArea: GlobalAreaType,
  searchValue:String,
  savePlanId:String
  form4Linght:any
}

// 创建搜索模块store
export const useSearchStore = defineStore({
  id: "search",
  state: (): SearchStateType => ({
    searchType: "1",
    orgType: [],
    professionType: [],
    addressTree: [],
    globalArea: {
      id: "19",
      label: "湖南省"
    },
    searchValue: '',
    savePlanId:'',
    form4Linght:['1']
  }),
  getters: {},
  actions: {
    changeStateSearchValue(val){
      this.searchValue = val
    },
    changeSearchType(type: "1" | "2" | "3" | "4") {
      this.searchType = type;
    },
    changeform4Linght(val:any){
      this.form4Linght = val
    },
    async getDictDatas() {
      // 发送网络请求获取字典数据
      const result = await main();
      const address = await getAddress()

      const { professionType, orgType } = result.data;

      this.orgType = orgType;
      this.professionType = professionType;
      this.addressTree = address.data

      localStorage.setItem("addressTree", JSON.stringify(address.data))
      localStorage.setItem("orgType", JSON.stringify(orgType))
      localStorage.setItem("professionType", JSON.stringify(professionType))
    },

    async changeGlobalArea(data: GlobalAreaType) {
      this.globalArea = {
        id: data.id,
        label: data.label
      }
    }
  }
});
